package com.ibangnuo.manage.config.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020-03-10 16:27
 */
@Aspect
@Component
public class BusinessDiaryAop {

    private static Map<String,String> classNameMap;
    private static Map<String,String> methodNameMap;

    /**
     * 类别名初始化
     */
    static {
        classNameMap = new HashMap<>();
        classNameMap.put("AgentService","代理商");
        classNameMap.put("AssistantService","店员");
        classNameMap.put("EmployeeService","员工");
    }

    /**
     * 方法别名初始化
     */
    static {
        methodNameMap = new HashMap<>();
        methodNameMap.put("add","添加");
        methodNameMap.put("find","查找");
        methodNameMap.put("update","修改");
        methodNameMap.put("edit","修改");
        methodNameMap.put("delete","删除");
    }

    @Pointcut("execution(public * com.ibangnuo.manage.service*..*(..))")
    public void pointcut() {
    }

    /**
     * 代理商，员工，店员业务执行记录
     * @param joinPoint
     */
    @After("pointcut()")
    public void beginBusinessEmployee(JoinPoint joinPoint) {
        Class<?> clazz = joinPoint.getTarget().getClass();
        String pageClassName = clazz.getName();
        String[] classNameList = pageClassName.split("\\.");
        String clssZhName = classNameMap.get(classNameList[classNameList.length - 1]);
        if(clssZhName == null){
            clssZhName = "";
        }
        String methodName = joinPoint.getSignature().getName();
        String methodZhName = "";
        for (Map.Entry<String, String> stringStringEntry : methodNameMap.entrySet()) {
            String methodNameKey = stringStringEntry.getKey();
            String methodNameValue = stringStringEntry.getValue();
            if((methodName.toLowerCase()).contains((methodNameKey))){
                methodZhName = methodNameValue;
            }

        }
        System.out.println("aop执行别名："+clssZhName+methodZhName);
    }
}
